﻿using System;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;
using System.Xml;
using PluginInterface;

namespace PluginDemo
{
    /// <summary>
    /// The demo Plug-in class inherits of UserControl and implements IPlugin.
    /// </summary>
    public partial class Uc_SFSO_Nomen : UserControl, IPlugin
    {
        #region Construction

        public Uc_SFSO_Nomen()
        {
            InitializeComponent();

            // Create grid view of proposed list of nomenclature
            try
            {
                SfoUrnInfoList sfoList = new SfoUrnInfoList();
                sfoList.DemoInit();
                dataGridViewSfo.DataSource = sfoList;
            }
            catch { }
        }
        #endregion

        #region IPlugin Members

        public event PluginEvent PluginEventExposed;

        public void Init(System.Xml.XmlDocument xDoc, object obExtend)
        {
            //Unused
        }

        public UserControl VisualComponent()
        {
            return this;
        }
        /// <summary> Signal no usage of initialization info.</summary>
        public bool RequiresInit
        {
            get { return false; }
        }

        #endregion

        #region Events

        private void dataGridViewSfo_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadNomenclature();

        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            LoadNomenclature();
        }
        /// <summary> Retrieves urn row selected in dataview </summary>
        /// <returns>The selocted object or null</returns>
        private SfoUrnInfo FindSelectedRow()
        {
            SfoUrnInfo findRow = null;
            BindingManagerBase bm = dataGridViewSfo.BindingContext[dataGridViewSfo.DataSource, dataGridViewSfo.DataMember];
            if (bm.Count > 0 && bm.Current != null)
                findRow = (SfoUrnInfo)bm.Current;

            return findRow;
        }

        /// <summary>
        /// Transmits selected nomnclature to main tree viev through event mechanism.
        /// </summary>
        private void LoadNomenclature()
        {
            // Grab the current, entry cursor
            // Set the current cursor to the wait and continue
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                SfoUrnInfo rowInfo = FindSelectedRow();
                if (rowInfo != null)
                {
                    // retrieve SDMX-ML content
                    Uri uri = new Uri(rowInfo.Url);
                    WebClient webclient = new WebClient();
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.Load(webclient.OpenRead(uri));

                    //Raise update event for main program
                    if (PluginEventExposed != null)
                        PluginEventExposed(this, new PluginEventArgs(xDoc));
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Plug-in Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // Swap the current cursor back to the original cursor
                Cursor.Current = currentCursor;
            }
        }

        private void buttonLogo_Click(object sender, EventArgs e)
        {
            try
            {
                Process myProc;
                myProc = Process.Start("http://www.bfs.admin.ch/bfs/portal/en/index.html");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        #endregion
    }  
}
